package com.atguigu.serviceedu.service.impl;

import com.atguigu.serviceedu.entity.EduCourse;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.feign.ServiceOssFeignClient;
import com.atguigu.serviceedu.mapper.EduCourseMapper;
import com.atguigu.serviceedu.mapper.EduTeacherMapper;
import com.atguigu.serviceedu.service.EduTeacherService;
import com.atguigu.vo.ResultVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2023-06-18
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    @Autowired
    private EduTeacherMapper eduTeacherMapper;
    @Autowired
    private ServiceOssFeignClient serviceOssFeignClient;
    @Autowired
    private EduCourseMapper eduCourseMapper;
    @Override
    public List<Map<String, Object>> queryteachernamebykeyword(String keyword) {
        QueryWrapper<EduTeacher> eduTeacherQueryWrapper = new QueryWrapper<>();
        eduTeacherQueryWrapper.select("name");//等价于select name from eudteacher
        eduTeacherQueryWrapper.likeRight("name",keyword);//等价于在上面的sql语句中增加了 where name like keyword
        List<Map<String, Object>> list = eduTeacherMapper.selectMaps(eduTeacherQueryWrapper);
        return list;
    }

    @Override
    public boolean deleteTeacherAvatarByTeacherid(String teacherid) {
        //首先前端传过来的是老师的id，你要先根据id拿到teacher,再拿到teacher对应的avatar
        EduTeacher eduTeacher = eduTeacherMapper.selectById(teacherid);
        if (eduTeacher !=null){
            //为什么是 !=null ? 因为你可能用这个id查不出来teacher，因为他可能先被另一个管理员删了，只不过你的前端页面上却还保留着他的id
            //如果你的eduTeacher是个null,那么你的eduteacher.getavatar()肯定报错空指针
            String avatarUrl = eduTeacher.getAvatar();
            if (avatarUrl !=null){
                //这里的 !=null 是因为你的teacher可能就没上传avatar
                ResultVO resultVO = serviceOssFeignClient.removeTeacherAvatarByUrl(avatarUrl);
                return  resultVO.isIsok();
            }
        }
        return false;
    }

    @Override
    public Map<String, Object> queryTeacherInfoById(String id) {
        EduTeacher teacher = eduTeacherMapper.selectById(id);
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("teacher_id",id);
        List<EduCourse> courses = eduCourseMapper.selectList(queryWrapper);
        HashMap<String, Object> map = new HashMap<>();
        map.put("teacher",teacher);
        map.put("courses",courses);
        return map;
    }

    @Override
    @Cacheable(value = "index",key = "'queryTeacherForSiteIndex'")
    public List<EduTeacher> queryTeacherForSiteIndex() {
        Page<EduTeacher> page = new Page<>(1,4);
        eduTeacherMapper.selectPage(page,null);
        List<EduTeacher> teachers = page.getRecords();
        return teachers;
    }
}
